package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//2019-12-16 09:48:28
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //添加新的角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds){

        try {
            roleService.add(role,permissionIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }

    }

    //分页查询
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){


        return roleService.findPage(queryPageBean);
    }

    //根据角色id查询角色信息
    @RequestMapping("findById")
    public Result findById(Integer id){

        try {
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }

    }

    //根据角色id查询权限的id集合
    @RequestMapping("/findPermissionIds")
    public Result findPermissionIds(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionIds(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //更新角色信息
    @RequestMapping("/editRole")
    public Result editRole(@RequestBody Role role,Integer[] permissionIds){

        try {
            roleService.editRole(role,permissionIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }


    //第一次删除角色
    @RequestMapping("/deleteFirst")
    public Result deleteFirst(Integer id){

        try {
            Result result = roleService.deleteFirst(id);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }

    }


    //第二次删除角色
    @RequestMapping("/deleteSecond")
    public Result deleteSecond(Integer id){

        try {
            roleService.deleteSecond(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //查询所有的角色
    @RequestMapping("/findAll")
    public Result findAll(){

        try {
            List<Role> roles = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }


}
